/**
 *====================================================
 * 文件名称: WarningSchemeResultServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2016年6月2日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.project.entity.WarningSchemeResultEntity;
import com.yuexunit.finance.project.entity.lexicon.SettlementStateEnum;
import com.yuexunit.finance.project.entity.lexicon.WarningFactorEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;

/**
 * @ClassName: WarningSchemeResultServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2016年6月2日 下午5:20:03
 */
@Implements(WarningSchemeResultService.class)
public class WarningSchemeResultServiceImpl implements WarningSchemeResultService {

	@Override
	public WarningSchemeResultEntity inquireWarningSchemeResult(Long schemeResultId) {
		return PersistUtil.get(WarningSchemeResultEntity.class, schemeResultId);
	}

	@Override
	public PageList<WarningSchemeResultEntity> inquireWarningSchemeResultPageList(List<Long> financeIds, WarningFactorEnum factor, Date startDate, Date endDate, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(WarningSchemeResultEntity.class);
		if (factor != null) {
			criteria.add(Restrictions.eq("factor", factor));
		}
		if (startDate != null) {
			criteria.add(Restrictions.ge("createDateTime", startDate));
		}
		if (endDate != null) {
			criteria.add(Restrictions.le("createDateTime", endDate));
		}
		if (financeIds != null) {
			criteria.add(Restrictions.in("financeId", financeIds));
		}
		criteria.add(Restrictions.or(Restrictions.eq("settlementState", SettlementStateEnum.UNKNOWN), Restrictions.isNull("settlementState")));
		criteria.addOrder(Order.desc("createDateTime"));
		return PersistUtil.get(criteria, page);
	}
	
	@Override
	public PageList<WarningSchemeResultEntity> inquireWarningSchemeResultSettledPageList(WarningFactorEnum factor, Date startDate, Date endDate,
			PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(WarningSchemeResultEntity.class);
		if (factor != null) {
			criteria.add(Restrictions.eq("factor", factor));
		}
		if (startDate != null) {
			criteria.add(Restrictions.ge("createDateTime", startDate));
		}
		if (endDate != null) {
			criteria.add(Restrictions.le("createDateTime", endDate));
		}
		criteria.add(Restrictions.gt("settlementState", SettlementStateEnum.UNKNOWN));
		return PersistUtil.get(criteria, page);
	}

	@Override
	public void updateSchemeResult(WarningSchemeResultEntity entity) {
		PersistUtil.update(entity);
	}

	@Override
	public List<Long> countSchemeResults(WarningFactorEnum factor) {
		DetachedCriteria criteria = DetachedCriteria.forClass(WarningSchemeResultEntity.class);
		criteria.add(Restrictions.eq("settlementState", SettlementStateEnum.UNKNOWN));
		criteria.add(Restrictions.eq("factor", factor));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("schemeResultId")));
		return PersistUtil.get(criteria);
	}

	@Override
	@Transactional
	public void createWarningSchemeResult(WarningSchemeResultEntity entity) {
		PersistUtil.insert(entity);
	}
}
